﻿using System;
namespace Doc.Xroad.Eu.Messages {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="GetDocumentResponse", Namespace="http://doc.x-road.eu/", Target=typeof(GetDocumentResponse))]
    public class GetDocumentResponseSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected GetDocumentResponse clazz;
        
        public GetDocumentResponseSerializer(GetDocumentResponse clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXmlAttributes(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXmlAttributes(Writer);
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("Document");
            if (clazz.Document!=null) {
                global::Xtee.Core.SerializationMetadata metaDocument = this.GetMetaData(clazz.Document);
                if (ShouldWriteTypeAttribute(typeof(global::Doc.Xroad.Eu.Document), clazz.Document.GetType())) {
                    Writer.WriteTypeAttribute(metaDocument.TypeQualifiedName.Namespace, metaDocument.TypeQualifiedName.Name);
                }
                var documentSerializer = metaDocument.NewSerializer(clazz.Document);
                documentSerializer.WriteXmlAttributes(Writer);
                documentSerializer.WriteXml(Writer);
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Document");
            }
            Writer.WriteEndElement();
        }
        
        public override void ReadXmlAttributes(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXmlAttributes(Reader);
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="Document") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>>>GetDocumentResponse>Document");
                }
                global::Xtee.Core.SerializationMetadata metaDocument = this.GetMetaData<global::Doc.Xroad.Eu.Document>(Reader, "Document", "http://doc.x-road.eu/");
                clazz.Document = metaDocument.NewEntity<global::Doc.Xroad.Eu.Document>();
                if ((Reader.IsEmptyElement == false)) {
                    var documentSerializer = metaDocument.NewSerializer(clazz.Document);
                    documentSerializer.ReadXmlAttributes(Reader);
                    Reader.ReadStartElement("Document");
                    documentSerializer.ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>>>GetDocumentResponse>Document", Reader.LocalName);
            }
        }
    }
}
